package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.mappers.EWSelfRaisMapper;
import com.burgeon.backend.EnterpriseWeChat.rest.FindMentionOrder;
import com.burgeon.backend.EnterpriseWeChat.rest.FindOrderDetails;
import com.burgeon.backend.EnterpriseWeChat.rest.FindWaitingOrder;
import com.burgeon.backend.EnterpriseWeChat.service.EWSelfRaisService;
import com.burgeon.backend.SHOP.entity.OrderItem;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVO.OrderEntityItem;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVO.OrderGoodsVO;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class EWSelfRaisServiceImpl implements EWSelfRaisService {

    @Autowired
    private EWSelfRaisMapper ewSelfRaisMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public List<OrderEntityItem> showWaitingOrder(String token, String store) throws Exception {

        Info info = GetInfoByToken(token);

        FindWaitingOrder findWaitingOrder = new FindWaitingOrder(info.getUrl(), info.getAccount(), info.getPassword());

        ArrayList<OrderEntityItem> dataAll = findWaitingOrder.getData(store);

        List<OrderEntityItem> data = new ArrayList<>();



        for (OrderEntityItem dataInfo:dataAll) {
            int price = 0;
            int orderId = ewSelfRaisMapper.findOrder(dataInfo.getXSNO(),token);
            List<OrderItem> goods = ewSelfRaisMapper.findOrderGoods(orderId);
            List<OrderGoodsVO> orderGoodsVOS = new ArrayList<>();

            for (OrderItem good:goods) {
                OrderGoodsVO orderGoodsVO = new OrderGoodsVO();
                orderGoodsVO.setImg(good.getGoodsImage());
                price += Integer.parseInt(good.getGoodsPrice());
                orderGoodsVOS.add(orderGoodsVO);
            }

            dataInfo.setPayment(Integer.toString(price));
            dataInfo.setOrderGoodsVOS(orderGoodsVOS);

            if(!data.contains(dataInfo)){
                data.add(dataInfo);
            }
        }
        System.err.println("===="+data);
        return data;
    }

    @Override
    public List<OrderEntityItem> showMentionOrder(String token, String store) throws Exception {
        Info info = GetInfoByToken(token);

        FindMentionOrder findMentionOrder = new FindMentionOrder(info.getUrl(), info.getAccount(), info.getPassword());

        List<OrderEntityItem> dataAll = findMentionOrder.getData(store);

        List<OrderEntityItem> data = new ArrayList<>();

        for (OrderEntityItem dataInfo:dataAll) {
            int price = 0;
            int orderId = ewSelfRaisMapper.findOrder(dataInfo.getXSNO(),token);
            List<OrderItem> goods = ewSelfRaisMapper.findOrderGoods(orderId);
            List<OrderGoodsVO> orderGoodsVOS = new ArrayList<>();

            for (OrderItem good:goods) {
                OrderGoodsVO orderGoodsVO = new OrderGoodsVO();
                orderGoodsVO.setImg(good.getGoodsImage());
                price += Integer.parseInt(good.getGoodsPrice());
                orderGoodsVOS.add(orderGoodsVO);
            }
            dataInfo.setPayment(Integer.toString(price));
            dataInfo.setOrderGoodsVOS(orderGoodsVOS);

            if(!data.contains(dataInfo)){
                data.add(dataInfo);
            }
        }
        System.err.println("===="+data);
        return data;
    }

    @Override
    public OrderEntityItem showOrderDetails(String token, String xsno) throws Exception {
        Info info = GetInfoByToken(token);
        FindOrderDetails findOrderDetails = new FindOrderDetails(info.getUrl(), info.getAccount(), info.getPassword());

        OrderEntityItem orderEntityItem = findOrderDetails.getData(xsno);

        int orderId = ewSelfRaisMapper.findOrder(orderEntityItem.getXSNO(),token);
        List<OrderItem> goods = ewSelfRaisMapper.findOrderGoods(orderId);
        List<OrderGoodsVO> orderGoodsVOS = new ArrayList<>();
        int totPrice = 0;//商品总额
        int discounts = 0;//优惠金额
        int payment = 0;//实付
        int integral = 0;//购物积分
        for (OrderItem good:goods) {
            OrderGoodsVO orderGoodsVO = new OrderGoodsVO();
            orderGoodsVO.setName(good.getGoodsTitle());
            orderGoodsVO.setPrice(good.getGoodsPrice());
            orderGoodsVO.setNum(good.getGoodsNum());
            orderGoodsVO.setCode(good.getCode());
            orderGoodsVO.setSpecifications(good.gettName());
            orderGoodsVO.setImg(good.getGoodsImage());
            orderGoodsVOS.add(orderGoodsVO);
            String original = ewSelfRaisMapper.findGoodOriginal(good.getGoodsId(),token);
            totPrice += Integer.parseInt(original);
            payment += Integer.parseInt(good.getGoodsPrice());
            integral += Integer.parseInt(good.getGoodsIntegral());
        }
        discounts = totPrice - payment;

        orderEntityItem.setPayment(Integer.toString(payment));
        orderEntityItem.setTotPrice(Integer.toString(totPrice));
        orderEntityItem.setDiscounts(Integer.toString(discounts));
        orderEntityItem.setIntegral(Integer.toString(integral));
        orderEntityItem.setOrderGoodsVOS(orderGoodsVOS);

        System.err.println("===="+orderEntityItem);
        return orderEntityItem;
    }

    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }
}
